package com.pptv.mtbu.ottflow.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.*;
import org.apache.struts2.interceptor.*;
import org.slf4j.Logger;


import com.pptv.mtbu.ottflow.service.*;
import com.pptv.mtbu.ottflow.domain.*;
import com.pptv.mtbu.ottflow.action.base.*;




public class LoginAction
    extends AdminBaseAction
{

    private final String CP_RESULT = "cp";
    private final String SP_RESULT = "sp";
    private final String ORM_RESULT = "orm";
    private final String ADMIN_RESULT = "admin";
    private final String SUPERADMIN_RESULT = "superadmin";



    private User user;

    private String vercode;

    private String tip;

    public void setUser(User user)
    {
        this.user = user;
    }
    public User getUser()
    {
        return this.user;
    }


    public void setVercode(String vercode)
    {
        this.vercode = vercode;
    }
    public String getVercode()
    {
        return this.vercode;
    }


    public void setTip(String tip)
    {
        System.out.println(tip);
        this.tip = tip;
    }
    public String getTip()
    {
        return this.tip;
    }

    public String execute()
        throws Exception
    {
        System.out.println("LoginAction execute");
        
        if(1 + 1 == 3){
            throw new Exception();
        }
   
        ActionContext ctx = ActionContext.getContext();

        String ver2 = (String)ctx.getSession().get(WebConstant.AUTHCODE);
        
        
        
        if (vercode.equalsIgnoreCase(ver2) || true)
        {
        	System.out.println("mgr:" + super.mgr);
            int result = super.mgr.validLogin(getUser());

            System.out.println("mgr.validLogin:" + result);

            switch (result) {
            case AdminManager.USER_SUPERADMIN:
                {
                    ctx.getSession().put(WebConstant.USER
                        , user.getUserName());
                    ctx.getSession().put(WebConstant.ROLE
                        , WebConstant.SUPERADMIN_ROLE);
                    setTip("用户是超级管理员，可以增加用户。");
                    return SUPERADMIN_RESULT;
                }
            case AdminManager.USER_CP:
                {
                    ctx.getSession().put(WebConstant.USER
                        , user.getUserName());
                    ctx.getSession().put(WebConstant.ROLE
                        , WebConstant.CP_ROLE);
                    setTip("用户是CP，可以提交节目。");
                    return CP_RESULT;
                }
            case AdminManager.USER_SP:
                {
                    ctx.getSession().put(WebConstant.USER
                        , user.getUserName());
                    ctx.getSession().put(WebConstant.ROLE
                        , WebConstant.SP_ROLE);
                    setTip("用户是SP，可以审核节目，并上线节目。");
                    return SP_RESULT;
                }
            case AdminManager.USER_ORM:
                {
                    ctx.getSession().put(WebConstant.USER
                        , user.getUserName());
                    ctx.getSession().put(WebConstant.ROLE
                        , WebConstant.ORM_ROLE);
                    setTip("用户是ORM，负责填写压制后的文件的地址。");
                    return SP_RESULT;
                }
            case AdminManager.USER_ADMIN:
                {
                    ctx.getSession().put(WebConstant.USER
                        , user.getUserName());
                    ctx.getSession().put(WebConstant.ROLE
                        , WebConstant.ADMIN_ROLE);
                    setTip("用户是admin，负责创建项目，并有除创建user外其他一切的权限。");
                    return ADMIN_RESULT;
                }

            default:
                {
                    setTip("该用户不存在");
                    return ERROR;
                }
            }
        }

        else
        {
            setTip("验证码错误");
            return ERROR;
        }
    }
}